<template>
  <el-card>
    <div slot="header" class="clearfix">
      <span>公会外交关系</span>
    </div>
    <el-form ref="diplomacyForm" :model="diplomacy" label-width="100px">
      <el-form-item label="其他公会ID">
        <el-input v-model="diplomacy.otherGuildId" placeholder="请输入其他公会ID"></el-input>
      </el-form-item>
      <el-form-item label="关系类型">
        <el-input v-model="diplomacy.relationType" placeholder="请输入关系类型"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="establishDiplomacy">建立关系</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="diplomacies" style="width: 100%; margin-top: 20px;">
      <el-table-column prop="id" label="关系ID" width="100"></el-table-column>
      <el-table-column prop="otherGuildId" label="其他公会ID"></el-table-column>
      <el-table-column prop="relationType" label="关系类型"></el-table-column>
      <el-table-column prop="relationTime" label="建立时间"></el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="scope">
          <el-button @click="updateDiplomacy(scope.row)" type="primary" size="small">编辑</el-button>
          <el-button @click="terminateDiplomacy(scope.row.id)" type="danger" size="small">终止</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { establishDiplomacy, getDiplomaciesByGuildId, updateGuildDiplomacy, terminateDiplomacy } from '@/utils/requests.js';

const diplomacy = ref({
  id: null,
  otherGuildId: '',
  relationType: '',
  relationTime: ''
});

const diplomacies = ref([]);

const loadDiplomacies = async () => {
  const response = await getDiplomaciesByGuildId(1); // 假设公会ID为1
  diplomacies.value = response.data;
};

onMounted(() => {
  loadDiplomacies();
});

const establishDiplomacy = async () => {
  await establishDiplomacy(diplomacy.value);
  loadDiplomacies();
};

const updateDiplomacy = (dip) => {
  // 实现更新外交关系逻辑
};

const terminateDiplomacy = async (id) => {
  await terminateDiplomacy(id);
  loadDiplomacies();
};
</script>